<?php
namespace App\Services;

use App\Models\Position;
use App\Models\StatisticsNum;

class UpdateStatisticsNumService {

    public static function handle() {
        $city_list = Position::where('status', 1)->groupBy('city')->select(\DB::raw('count(city) as total, city'))->get();
        $type_list = Position::where('status', 1)->groupBy('type')->select(\DB::raw('count(type) as total, type'))->get();

        StatisticsNum::truncate();
        foreach ($city_list as $key => $value) {
            StatisticsNum::create([
                'type' => StatisticsNum::TYPE_CITY,
                'title' => $value->city,
                'num' => $value->total
            ]);
        }
        foreach ($type_list as $key => $value) {
            StatisticsNum::create([
                'type' => StatisticsNum::TYPE_POSITION,
                'title' => $value->type,
                'num' => $value->total
            ]);
        }
    }

}
